<?php
namespace app\admin\service;

use function think\where;
use \think\Db;
use think\Session;
use com\Des;
class DepositService
{
    public function index($filter)
    {
        $deposit = model('deposit');
        
        $where = [
            
        ];        
        $join = [

        ];
         
        $field = 'a.*';


        //条件查询
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }
        //过滤收款人消费数据代码
        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.vip_no'] = ['like', "%$keyword%"];
        }
        $deposit = $deposit->where($where)->join($join)->alias('a')->field($field)->order('id','DESC')->paginate(10,false,['query'=>request()->param() ]);
        return $deposit;    
    }
    //消费数据充值
    public function silverIndex($filter)
    {
        $deposit = model('tb_vip_info');
    
        $where = [
    
        ];
        $join = [
            ['__MEMBER__ member','a.isid = member.id'],
        ];
        $field = 'a.*,member.real_name';            
                
        $des   = new Des();
        $keyword = isset($filter['vip_no'])?$filter['vip_no']:null;
        $qrcodeStr = $des->decrypt($keyword);
        if(($qrcodeStr!== false)){
            
            $qrcode    = json_decode($qrcodeStr,true);
            
            if(!empty($qrcode['time'] && ($qrcode['time']+120>time()))){
                $keyword   = $qrcode['vip'];
                $where['a.vip_no'] = $keyword;
                
                $silver = $deposit->where($where)->join($join)->alias('a')->field($field)->select();
            }else {
                $silver =null;
            }
        
        }else {
            $silver =null;
        }
        return $silver;
    }
    //消费数据充值
    public function silverPost($vipNo, $score, $payType){
        $transStatus = false;
        $deposit['vip_no']      = $vipNo;
        $admin = Session::get('admin');
        $deposit['deposit_operator'] = empty($admin['name'])?'':$admin['name'];//操作人员
        $deposit['money']       = $score;
        $deposit['pay_type']       = $payType;
        $deposit['deposit_type']       = 'hyxt';
        $deposit['create_time'] = time();
    
        Db::startTrans();
        try{
            Db::name('tb_vip_info')->where('vip_no',$vipNo)->setInc('vip_score_y', $score);
            //充值记录
            Db::name('deposit') ->insert($deposit);
            $transStatus = true;
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            \think\Log::record($e->getMessage());
            // 回滚事务
            Db::rollback();
        }
        return $transStatus;
    }
	//消费充值列表
    public function silverList($filter,$groupId)
    {
        $deposit = model('deposit');
        $admin = Session::get('admin');
		if($groupId===1){
			$where = [
            //'deposit_operator'=>$admin['name']
        ];
		}else{
			$where = [
            'deposit_operator'=>$admin['name']
        ];
		}
        
		
        //条件查询
        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.create_time'] = ['<= time', $endTime];
            }
        }
        
        $join = [
            ['__MEMBER_DETAIL__ detail','a.vip_no = detail.silver_card'],
            ['__MEMBER__ member','member.id = detail.mid'],
        ];
        $field = 'a.*,member.real_name';
        
        //过滤收款人消费数据代码
        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.vip_no'] = $keyword;
        }
    
        $silver['data']   = Db::name('deposit')->where($where)->join($join)->alias('a')->order('id','DESC')->field($field)->paginate(10,false,['query'=>request()->param() ]);
        $silver['amount'] = Db::name('deposit')->where($where)->alias('a')->sum('money');
        return $silver;
    }
}

